//
//  QTagView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/7.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QTagView.h"

@implementation QTagView


- (void)setTitleArray:(NSArray *)titleArray {
    
    _titleArray = titleArray;
    for (NSString *title in _titleArray) {
        QTagLabel *tagLabel = [[QTagLabel alloc] init];
        tagLabel.layer.masksToBounds = YES;
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.text = title;
        [self addSubview:tagLabel];
    }
    
    _labelArr = [NSMutableArray array];
    _selectDic = [NSMutableDictionary dictionary];
    _blockArr = [NSMutableArray array];
    
    if (self.subviews.count) {
        
        CGFloat padding = 12.0f;
        CGFloat lastLabelRight = 0;
        CGFloat tagLabelW = 0;
        CGFloat tagLabelH = 0;
        NSInteger row = 0;
        
        for (int i=0; i<_titleArray.count; i++) {
            QTagLabel *tagLabel = self.subviews[i];
            tagLabel.tag = i;
            tagLabel.font = _tagLabelFont;
            tagLabel.textColor = _tagLabelTextColor;
            tagLabel.layer.borderColor = _tagLabelBorderColor.CGColor;
            tagLabel.layer.borderWidth = _tagLabelBorderWidth;
            tagLabel.layer.cornerRadius = _tagLabelCornerRadius;
            tagLabel.widthPadding = _tagLabelWidthPadding;
            tagLabel.heightPadding = _tagLabelHeightPadding;
            tagLabelW = tagLabel.intrinsicContentSize.width;
            tagLabelH = tagLabel.intrinsicContentSize.height;
            tagLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taps:)];
            [tagLabel addGestureRecognizer:tap];
            if ((lastLabelRight + padding + tagLabelW) > (self.bounds.size.width - padding)) { //需要换行
                row++;
                lastLabelRight = 0;
            }
            CGFloat tagLabelX = lastLabelRight + padding;
            CGFloat tagLabelY = row * tagLabelH + (row+1) * padding;
            tagLabel.frame = CGRectMake(tagLabelX, tagLabelY, tagLabelW, tagLabelH);
            lastLabelRight = tagLabelX + tagLabelW;
            [_labelArr addObject:tagLabel];
        }
        
        _viewHeight = (row+1) * tagLabelH + (row+2)*padding;
    }
}

- (void)taps:(UIGestureRecognizer *)tap{
    
    QTagLabel *label = (QTagLabel *)tap.view;
    if(label.isSelect){
        [_selectDic removeObjectForKey:[NSString stringWithFormat:@"%ld",(long)label.tag]];
    }else{
        [_selectDic setObject:label forKey:[NSString stringWithFormat:@"%ld",(long)label.tag]];
    }
    label.isSelect = !label.isSelect;
    
    for(QTagLabel *l in _labelArr){//****************************先要恢复所有的颜色
        l.textColor = _tagLabelTextColor;
        l.layer.borderColor = _tagLabelBorderColor.CGColor;
        l.layer.borderWidth = _tagLabelBorderWidth;
    }
    [_blockArr removeAllObjects];
    for (NSString *key in _selectDic) {//***********************改变选中的颜色
        QTagLabel *lab = [_selectDic objectForKey:key];
        lab.textColor = [QColorUtility defaultBlueColor];
        lab.layer.borderColor = [QColorUtility defaultBlueColor].CGColor;
        lab.layer.borderWidth = _tagLabelBorderWidth+0.5;
        [_blockArr addObject:lab];
    }
    
    if(_RequestBlock){
        _RequestBlock((NSArray *)_blockArr);
    }
}

@end
